package com.spa.infrastructure.repository.service;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spa.domain.entity.Trade;
import com.spa.infrastructure.mapper.TradeMapperStruct;
import com.spa.infrastructure.repository.condition.TradeCondition;
import com.spa.infrastructure.repository.mapper.TradeMapper;
import com.spa.infrastructure.repository.po.TradePO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Slf4j
public class TradeRepositoryService extends ServiceImpl<TradeMapper, TradePO> {

    @Autowired
    private TradeMapper tradeMapper;

    public Long add(TradePO tradePO) {
        tradeMapper.insert(tradePO);
        return tradePO.getId();
    }

    /**
     * 根据条件列出交易信息。
     * @param condition 交易条件，用于查询过滤。
     * @return 返回交易实体的列表。
     */
    public List<Trade> list(TradeCondition condition) {
        LambdaQueryWrapper<TradePO> queryWrapper = getQueryWrapper(condition);
        List<TradePO> tradePOList = tradeMapper.selectList(queryWrapper);
        return TradeMapperStruct.INSTANCE.po2EntityList(tradePOList);
    }

    /**
     * 根据交易条件构建查询Wrapper。
     * @param condition 交易条件对象，包含订单号等查询条件。
     * @return 返回构建好的LambdaQueryWrapper对象，用于后续的数据库查询。
     */
    private LambdaQueryWrapper<TradePO> getQueryWrapper(TradeCondition condition) {
        LambdaQueryWrapper<TradePO> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.in(CollUtil.isNotEmpty(condition.getOrderCodes()),TradePO::getOutTradeNo,condition.getOrderCodes());
        return queryWrapper;
    }



}
